package com.lagou.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lagou.dao.MenuMapper;
import com.lagou.domain.Menu;
import com.lagou.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuMapper menuMapper;

    /**
     * 查询所有菜单列表
     *
     * @param pid 父级目录id
     * @return
     */
    @Override
    public List<Menu> findSubMenuListByPid(Integer pid) {

        return menuMapper.findSubMenuListByPid(pid);
    }

    /**
     * 根据角色ID查询关联菜单ID
     *
     * @param roleId 角色id
     * @return
     */
    @Override
    public List<Integer> findMenuByRoleId(Integer roleId) {

        return menuMapper.findMenuByRoleId(roleId);
    }

    /**
     * 菜单列表查询
     *
     * @return
     * @param currentPage
     * @param pageSize
     */
    @Override
    public PageInfo<Menu> findAllMenu(Integer currentPage, Integer pageSize) {

        PageHelper.startPage(currentPage,pageSize);
        List<Menu> list=menuMapper.findAllMenu();
        return new PageInfo<>(list);
    }

    /**
     * 根据菜单id查询菜单信息
     *
     * @param id 菜单id
     * @return
     */
    @Override
    public Menu findMenuById(Integer id) {

        return menuMapper.findMenuById(id);
    }

    /**
     * 添加菜单
     *
     * @param menu
     */
    @Override
    public void saveMenu(Menu menu) {

        Date date = new Date();
        menu.setCreatedBy("system");
        menu.setCreatedTime(date);
        menu.setUpdatedBy("system");
        menu.setUpdatedTime(date);
        menuMapper.saveMenu(menu);
    }

    /**
     * 修改菜单
     *
     * @param menu
     */
    @Override
    public void updateMenu(Menu menu) {

        menuMapper.updateMenu(menu);
    }

}
